﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace('jQueryNET.Controls');

jQueryNET.Controls.Button = function jQueryNET$Controls$Button(element) {
	/// <summary>
	/// This class implement Button control
	/// </summary>
	/// <param name='element' type='Sys.UI.DomElement' domElement='true'>
	/// DOM element the control is associated with
	/// </param>
	
	jQueryNET.Controls.Button.initializeBase(this, [element]);
	//this._element = $(this.get_element());
	
	this._rootElement.addClass('button');

	this._rootElement.attr({
		cellspacing:0,
		cellpadding:0,
		border:0,
		style: 'width: auto;'
	});

	this._rootElement.append(
		'<tbody>'+
			'<tr>'+
				'<td class="button_left"><i> </i></td>'+
				'<td class="button_center">'+
					'<em unselectable="on">'+
						'<button class="button_text" type="button">' +
						'</button>' +
					'</em>'+
				'</td>'+
				'<td class="button_right"><i> </i></td>' +
			'</tr>'+
		'</tbody>');

	this._buttonElement = this._rootElement.find('button');

	this._onMouseOverHandler = Function.eventWraper(this, this._onMouseOver)
	this._onMouseOutHandler = Function.eventWraper(this, this._onMouseOut)
	this._onMouseDownHandler = Function.eventWraper(this, this._onMouseDown)
	this._onMouseUpHandler = Function.eventWraper(this, this._onMouseUp)
	this._onClickHandler = Function.eventWraper(this, this._onClick)
	this._onFocusHandler = Function.eventWraper(this, this._onFocus)
	this._onBlurHandler = Function.eventWraper(this, this._onBlur)
}

jQueryNET.Controls.Button.createElement = function(parent) {
	/// <summary>
	/// Static method for using on client-side. Create DOM element the control
	/// associated with
	/// </summary>
	/// <param name='parent' type='Sys.UI.DomElement' domElement='true'>
	/// Parent DOM element or jQuery selector
	/// </param>
	
	var element =
		$(parent)
			.append('<table></table>')
			.find('table:last');

	return element.get(0);}

function jQueryNET$Controls$Button$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>

	jQueryNET.Controls.Button.callBaseMethod(this, 'initialize');

	this._rootElement
		.hover(this._onMouseOverHandler, this._onMouseOutHandler)
		.bind('mousedown', this, this._onMouseDownHandler)
		.bind('mouseup', this, this._onMouseUpHandler)
		.bind('click', this, this._onClickHandler);
	this._buttonElement
		.bind('focus', this, this._onFocusHandler)
		.bind('blur', this, this._onBlurHandler);	
}

function jQueryNET$Controls$Button$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>
	
	this._rootElement
		.unbind('mousedown', this, this._onMouseDownHandler)
		.unbind('mouseup', this, this._onMouseUpHandler)
		.unbind('click', this, this._onClickHandler);
	this._buttonElement
		.unbind('focus', this, this._onFocusHandler)
		.unbind('blur', this, this._onBlurHandler);

	jQueryNET.Controls.Button.callBaseMethod(this, 'dispose');
}

function jQueryNET$Controls$Button$get_title() {
	/// <value type='String'>
	/// Button title
	/// </value>
	
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._title;
}

function jQueryNET$Controls$Button$set_title(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: false}]);
	if (e) throw e;
	this._title = value;

	if (this._title) {
		this._rootElement.addClass('button_text');
		this._buttonElement.text(this._title);
	}
	else {
		this._rootElement.removeClass('button_text');
		this._buttonElement.text();
	}
}

function jQueryNET$Controls$Button$get_imageUrl() {
	/// <value type='String'>
	/// Button icon url
	/// </value>
	
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._imageUrl;
}

function jQueryNET$Controls$Button$set_imageUrl(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: false}]);
	if (e) throw e;
	this._imageUrl = value;

	if (this._imageUrl) {
		this._buttonElement.attr('style', 'background-image:url(' + this._imageUrl + ')');
		this._rootElement.addClass('button_icon');
	}
	else {
		this._buttonElement.removeAttr('style');
		if (!this.get_imageClass())
			this._rootElement.removeClass('button_icon');
	}
}

function jQueryNET$Controls$Button$get_imageClass() {
	/// <value type='String'>
	/// Button icon class
	/// </value>
	
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._imageClass;
}

function jQueryNET$Controls$Button$set_imageClass(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: false}]);
	if (e) throw e;

	if (value) {
		this._buttonElement.addClass(value);
		this._rootElement.addClass('button_icon');
	}
	else {
		this._buttonElement.removeClass(this._imageClass)
		if (!this.get_imageUrl())
			this._rootElement.removeClass('button_icon');
	}
		
	this._imageClass = value;
}

function jQueryNET$Controls$Button$get_enabled() {
	/// <value type='Boolean'>
	/// Enabled button state
	/// </value>
	
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._enabled;
}

function jQueryNET$Controls$Button$set_enabled(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Boolean, mayBeNull: false}]);
	if (e) throw e;
	this._enabled = value;
	
	if (this._enabled) {
		this._rootElement
			.removeAttr('disabled')
			.removeClass('disabled');
	}
	else {
		this._rootElement
			.attr('disabled', 'disabled')
			.addClass('disabled')
			.removeClass('button_over');
	}
}

function jQueryNET$Controls$Button$get_pressed() {
	/// <value type='Boolean'>
	/// Pressed button state
	/// </value>
	
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._pressed;
}

function jQueryNET$Controls$Button$set_pressed(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Boolean, mayBeNull: false}]);
	if (e) throw e;
	this._pressed = value;
}

function jQueryNET$Controls$Button$get_enableToggle() {
	/// <value type='Boolean'>
	/// Enable toggle button state
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._enableToggle;
}

function jQueryNET$Controls$Button$set_enableToggle(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Boolean, mayBeNull: false}]);
	if (e) throw e;
	this._enableToggle = value;
}

function jQueryNET$Controls$Button$add_click(handler) {
	/// <summary>
	/// Add an event handler for the click event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().addHandler('click', handler);
}

function jQueryNET$Controls$Button$remove_click(handler) {
	/// <summary>
	/// Remove an event handler from the click event
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />

	this.get_events().removeHandler('click', handler);
}

function jQueryNET$Controls$Button$raiseClick(eventArgs) {
	/// <summary>
	/// Raise the click event
	/// </summary>
	/// <param name='eventArgs' type='Sys.CancelEventArgs' mayBeNull='false'>
	/// Event arguments for the click event
	/// </param>
	/// <returns />

	var handler = this.get_events().getHandler('click');
	if (handler) {
		handler(this, eventArgs);
	}
}

function jQueryNET$Controls$Button$_onMouseOver(event, sender) {
	/// <summary>
	/// Get the left offset of the popup
	/// </summary>
	/// <returns type='Number' integer='true'>
	/// Left offset of the popup (in pixels)
	/// </returns>

	$(sender).addClass('button_over');
}

function jQueryNET$Controls$Button$_onMouseOut(event, sender) {
	/// <summary>
	/// Event handler for the mouseover event
	/// </summary>

	$(sender).removeClass('button_over');
}

function jQueryNET$Controls$Button$_onMouseDown(event,sender) {
	/// <summary>
	/// Event handler for the mousedown event
	/// </summary>

	$(sender).addClass('button_click');
}

function jQueryNET$Controls$Button$_onMouseUp(event, sender) {
	/// <summary>
	/// Event handler for the mouseup event
	/// </summary>

	$(sender).removeClass('button_click');
}

function jQueryNET$Controls$Button$_onClick(event, sender) {
	/// <summary>
	/// Event handler for the click event
	/// </summary>

	if (this.get_enableToggle())
		$(sender).toggleClass('button_pressed');
		
	this.raiseClick(new Sys.EventArgs());
}

function jQueryNET$Controls$Button$_onFocus(event, sender) {
	/// <summary>
	/// Event handler for the focus event
	/// </summary>

	this._rootElement.addClass('button_focus');
}

function jQueryNET$Controls$Button$_onBlue(event, sender) {
	/// <summary>
	/// Event handler for the blue event
	/// </summary>

	this._rootElement.removeClass('button_focus');
}

jQueryNET.Controls.Button.prototype = {
	_buttonElement: null,
	
	_title: '',
	_imageUrl: null,
	_imageClass: null,
	_enabled: true,
	_pressed: false,
	_enableToggle: false,

	initialize: jQueryNET$Controls$Button$initialize,
	dispose: jQueryNET$Controls$Button$dispose,

	get_title: jQueryNET$Controls$Button$get_title,
	set_title: jQueryNET$Controls$Button$set_title,
	get_imageUrl: jQueryNET$Controls$Button$get_imageUrl,
	set_imageUrl: jQueryNET$Controls$Button$set_imageUrl,
	get_imageClass: jQueryNET$Controls$Button$get_imageClass,
	set_imageClass: jQueryNET$Controls$Button$set_imageClass,
	get_enabled: jQueryNET$Controls$Button$get_enabled,
	set_enabled: jQueryNET$Controls$Button$set_enabled,
	get_pressed: jQueryNET$Controls$Button$get_pressed,
	set_pressed: jQueryNET$Controls$Button$set_pressed,
	get_enableToggle: jQueryNET$Controls$Button$get_enableToggle,
	set_enableToggle: jQueryNET$Controls$Button$set_enableToggle,
	
	add_click: jQueryNET$Controls$Button$add_click,
	remove_click: jQueryNET$Controls$Button$remove_click,
	raiseClick: jQueryNET$Controls$Button$raiseClick,

	_onMouseOver: jQueryNET$Controls$Button$_onMouseOver,
	_onMouseOut: jQueryNET$Controls$Button$_onMouseOut,
	_onMouseDown: jQueryNET$Controls$Button$_onMouseDown,
	_onMouseUp: jQueryNET$Controls$Button$_onMouseUp,
	_onClick: jQueryNET$Controls$Button$_onClick,
	_onFocus: jQueryNET$Controls$Button$_onFocus,
	_onBlur: jQueryNET$Controls$Button$_onBlue
}

jQueryNET.Controls.Button.registerClass('jQueryNET.Controls.Button', jQueryNET.Controls.ControlBase);
